package cn.iocoder.an.module.mold.dal.mysql.equipmnetkeepwait;

import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.an.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.an.module.mold.controller.admin.equipmnetkeepwait.vo.EquipmnetKeepWaitPageReqVO;
import cn.iocoder.an.module.mold.dal.dataobject.equipmnetkeepwait.EquipmnetKeepWaitDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

/**
 * 待保养设备 Mapper
 *
 * @author zzy
 */
@Mapper
@Repository("EquipmnetKeepWaitMapperDamage")
public interface EquipmnetKeepWaitMapper extends BaseMapperX<EquipmnetKeepWaitDO> {

    default PageResult<EquipmnetKeepWaitDO> selectPage(EquipmnetKeepWaitPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<EquipmnetKeepWaitDO>()
                .likeIfPresent(EquipmnetKeepWaitDO::getKeepNo, reqVO.getKeepNo())
                .likeIfPresent(EquipmnetKeepWaitDO::getEquipmentName, reqVO.getEquipmentName())
                .eqIfPresent(EquipmnetKeepWaitDO::getItemId, reqVO.getItemId())
                .eqIfPresent(EquipmnetKeepWaitDO::getEquipmentId, reqVO.getEquipmentId())
                .eqIfPresent(EquipmnetKeepWaitDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(EquipmnetKeepWaitDO::getPlanDate, reqVO.getPlanDate())
                .likeIfPresent(EquipmnetKeepWaitDO::getDutyUserName, reqVO.getDutyUserName())
                .inIfPresent(EquipmnetKeepWaitDO::getId,reqVO.getIds())
                .inIfPresent(EquipmnetKeepWaitDO::getEquipmentId,reqVO.getIdList())
                .neIfPresent(EquipmnetKeepWaitDO::getStatus, reqVO.getExcludeStatus())
                .orderByAsc(EquipmnetKeepWaitDO::getPlanDate));
    }


    default PageResult<EquipmnetKeepWaitDO> selectLatestByEquipmentId(EquipmnetKeepWaitPageReqVO reqVO) {
        MPJLambdaWrapper<EquipmnetKeepWaitDO> wrapper = new MPJLambdaWrapper<EquipmnetKeepWaitDO>()
                .select( EquipmnetKeepWaitDO::getId,
                        EquipmnetKeepWaitDO::getKeepNo,
                        EquipmnetKeepWaitDO::getEquipmentId,
                        EquipmnetKeepWaitDO::getEquipmentName,
                        EquipmnetKeepWaitDO::getItemId,
                        EquipmnetKeepWaitDO::getDutyUserId,
                        EquipmnetKeepWaitDO::getDutyUserName,
                        EquipmnetKeepWaitDO::getStatus,
                        EquipmnetKeepWaitDO::getPlanDate,
                        EquipmnetKeepWaitDO::getRemaining,
                        EquipmnetKeepWaitDO::getSendTime,
                        EquipmnetKeepWaitDO::getCreator,
                        EquipmnetKeepWaitDO::getCreateTime,
                        EquipmnetKeepWaitDO::getUpdater,
                        EquipmnetKeepWaitDO::getUpdateTime
                )
                .eqIfExists(EquipmnetKeepWaitDO::getItemId, reqVO.getItemId())
                .likeIfExists(EquipmnetKeepWaitDO::getEquipmentName, reqVO.getEquipmentName())
                .eqIfExists(EquipmnetKeepWaitDO::getStatus, reqVO.getStatus())
                .likeIfExists(EquipmnetKeepWaitDO::getDutyUserName, reqVO.getDutyUserName())
                .apply(" t.update_time = (" +
                        "    SELECT MAX(r.update_time) " +
                        "    FROM business_equipmnet_damage_wait r " +
                        "    WHERE r.equipment_id = t.equipment_id" +
                        "    AND r.tenant_id = t.tenant_id" +
                        ")");

        // 分页查询
        return selectPage(reqVO, wrapper);
    }






    @Select("SELECT MAX(keep_no) FROM business_equipmnet_keep_wait WHERE keep_no LIKE CONCAT(#{prefix}, '%')")
    String findMaxKeepNo(@Param("prefix") String prefix);

}